/** Log window UI element.
@param buffer The ScreenBuffer object to render to.
@param x X offset.
@param y Y offset.
@param width Width.
@param height Height.
@param depth How many turns worth of messages to remember in the history buffer.
*/
function MessageLogMode(buffer, x, y, width, height, depth)
{
	this.buffer = buffer;
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	this.history = [];
	for(var i = 0; i < depth; ++i)
	{
		this.history.push("");
	}
	this.historyIndex = 0;
	this.top = 0;
	this.lines = [];
	this.isTempMessage = false;
	this.isClear = false;
}
MessageLogMode.prototype =
{
	/** Handle actions for the log window. This is always called before any
	 *other handler when in game mode.
	 *@param action The action string to handle.
	 */
	handleAction: function(action)
	{
		var hidxChanged = false;
		switch(action)
		{
			case "logup": --this.top; break;
			case "logdown": ++this.top; break;
			case "historyup": ++this.historyIndex; hidxChanged = true; break;
			case "historydown": --this.historyIndex; hidxChanged = true; break;
			default:
				return false;
				break;
		}
		if(hidxChanged)
		{
			if(this.historyIndex < 0)
				this.historyIndex = 0;
			if(this.historyIndex >= this.history.length)
				this.historyIndex = this.history.length - 1;
			else
			{
				if(this.historyIndex == 1 &&
					this.lines.length == 0 )
					this.historyIndex = 0;
				var idx = this.historyIndex;
				this.clearMessage();
				this.historyIndex = idx;
				var lines = this.history[(this.history.length - 1) - this.historyIndex].split("\n");
				for(var i = 0; i < lines.length; ++i)
					this.pushMessage(lines[i], true);
				this.isTempMessage = true;
				this.historyIndex = idx;
			}
		}
		else
		{
			if(this.top + this.height > this.lines.length)
				this.top = this.lines.length - this.height;
			if(this.top < 0)
				this.top = 0;
		}
		this.render(true);
		return true;
	},
	/** Clears the current message and pushes it onto the history buffer.
	 */
	clearMessage: function()
	{
		if(!this.isTempMessage &&
			this.lines.length > 0 &&
			this.lines[0].length > 0)
		{
			this.history.push(this.lines.join("\n"));
			this.history.splice(0, 1);
		}
		this.historyIndex = 0;
		this.lines = [];
		this.top = 0;
		this.render(true);
	},
	/** Replace the current message with a temporary message.
	 *@param str The message to display.
	 */
	tempMessage: function(str)
	{
		this.isTempMessage = true;
		this.pushMessage(str);
		this.isTempMessage = true;
	},
	/** Push a message onto the log.
	@param str Message string.
	@param doNotRender If true skip rendering the log window.
	*/
	pushMessage: function(str, doNotRender)
	{
		if(this.isTempMessage)
		{
			this.clearMessage();
			this.isTempMessage = false;
		}
		if(str.length > 0)
		{
			var strLines = util.wordWrap(str, this.width - 1);
			for(var i = 0; i < strLines.length; ++i)
			{
				this.lines.push(strLines[i]);
			}
		}
		if(!doNotRender)
			this.render(true);
	},
	/** Returns true if the log is currently empty.
	 */
	isEmpty: function()
	{
		return this.lines.length == 0;
	},
	/** Redraws the log display on the root window.
	 *@param isUpdate True if this was called internally as an update.
	 */
	render: function(isUpdate)
	{
		var i, line;
		
		// Render lines
		for(i = 0; i < this.height; ++i)
		{
			if(i + this.top >= this.lines.length)
			{
				if(!this.isClear ||
					!this.isUpdate)
					this.buffer.blitRect(this.x, this.y + i, this.width, 1, " ", null, Color.black);
				continue;
			}
			line = this.lines[i + this.top];
			this.buffer.blitString(this.x, this.y + i, line, this.width - 1);
		}
		if(this.lines.length == 0)
			this.isClear = true;
		else
			this.isClear = false;
		
		// Render scroll indicators if needed
		if(this.top > 0 ||
			this.top + this.height < this.lines.length)
			this.buffer.blitRect(this.x + (this.width - 1), this.y, 1, this.height, Char.DBAR_NS, Color.black, Color.white);
		else
			this.buffer.blitRect(this.x + (this.width - 1), this.y, 1, this.height, " ", Color.black, Color.black);
		if(this.top > 0)
			this.buffer.setCell(this.x + (this.width - 1), this.y, Char.POINTER_UP, null, null);
		if(this.top + this.height < this.lines.length)
			this.buffer.setCell(this.x + (this.width - 1), this.y + (this.height - 1), Char.POINTER_DOWN, null, null);
	}
};
